<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${configuration.javaModelPackage}.mapper.${configuration.domainObjectName}Mapper">

    <resultMap type="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}"
               id="${configuration.domainObjectName}Map">
        <#list columnList as column>
            <result column="${column.javaProperty}" property="${column.actualColumnName}"
                    jdbcType="${column.jdbcTypeName}"/>
        </#list>
    </resultMap>

    <!-- 条件  -->
    <sql id="Example_Where_Clause">
        <trim prefix="where" prefixOverrides="and|or">
            <#list columnList as column>
                <if test="${column.javaProperty} != null and ${column.javaProperty} != ''">
                    and ${column.actualColumnName} = ${r"#"}{${column.javaProperty}}
                </if>
            </#list>
        </trim>
    </sql>

    <!-- 数据库字段 -->
    <sql id="Base_Column_List">
        <#list columnList as column>
            ${column.actualColumnName}<#if column_has_next>,</#if>
        </#list>
    </sql>

    <!-- 获取全部 -->
    <select id="getList" resultMap="${configuration.domainObjectName}Map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${configuration.tableName}
    </select>

    <!-- 根据条件获取符合条件的数据  -->
    <select id="getPageList" resultMap="${configuration.domainObjectName}Map"
            parameterType="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${configuration.tableName}
        <include refid="Example_Where_Clause"/>
    </select>

    <!-- 根据条件获取符合条件的数据 -->
    <select id="getPageListByMap" resultMap="${configuration.domainObjectName}Map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${configuration.tableName}
        <include refid="Example_Where_Clause"/>
    </select>

    <!-- 根据主键获取数据 -->
    <select id="selectByPrimaryKey" parameterType="${configuration.pkTypeJava}"
            resultMap="${configuration.domainObjectName}Map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${configuration.tableName}
        WHERE ${configuration.pkName} = ${r"#"}{${configuration.pkSqlName}}
    </select>

    <!-- 根据条件获取数据 -->
    <select id="selectBy" parameterType="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}"
            resultMap="${configuration.domainObjectName}Map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${configuration.tableName}
        <include refid="Example_Where_Clause"/>
    </select>

    <!-- 插入一条数据 -->
    <insert id="insert" parameterType="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}"
            keyProperty="${configuration.pkName}" useGeneratedKeys="true">
        INSERT INTO ${configuration.tableName}(
        <include refid="Base_Column_List"/>
        )
        values (
        <#list columnList as column>
            ${r"#"}{${column.javaProperty}}<#if column_has_next>,</#if>
        </#list>
        )
    </insert>

    <!-- 插入有值的字段数据 -->
    <insert id="createSelective"
            parameterType="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}"
            keyProperty="${configuration.pkName}" useGeneratedKeys="true">
        INSERT INTO ${configuration.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list columnList as column>
                <if test="${column.actualColumnName} != null">
                    ${column.actualColumnName},
                </if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list columnList as column>
                <if test="${column.javaProperty} != null">
                    ${r"#"}{${column.javaProperty}},
                </if>
            </#list>
        </trim>
    </insert>

    <!-- 批量插入有值的字段数据 -->
    <insert id="createSelectiveBatch" parameterType="java.util.List" keyProperty="${configuration.pkName}"
            useGeneratedKeys="true">
        INSERT INTO ${configuration.tableName}
        (
        <#list columnList as column>
            ${column.actualColumnName}<#if column_has_next>,</#if>
        </#list>
        )
        values
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            (
            <#list columnList as column>
                ${r"#"}{item.${column.javaProperty}}<#if column_has_next>,</#if>
            </#list>
            )
        </foreach>
    </insert>

    <!-- 根据ID更新数据 -->
    <update id="update" parameterType="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}">
        UPDATE ${configuration.tableName}
        <set>
            <#list columnList as column>
                ${column.actualColumnName} = ${r"#"}{${column.javaProperty}}<#if column_has_next>,</#if>
            </#list>
        </set>
        WHERE ${configuration.pkSqlName} = ${r"#"}{${configuration.pkName}}
    </update>

    <!-- 根据ID更新有值的字段数据 -->
    <update id="updateSelective"
            parameterType="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}">
        UPDATE ${configuration.tableName}
        <set>
            <#list columnList as column>
                <#if (configuration.pkSqlName != column.actualColumnName)>
                    <if test="${column.javaProperty} != null">
                        ${column.actualColumnName} = ${r"#"}{${column.javaProperty}}<#if column_has_next>,</#if>
                    </if>
                </#if>
            </#list>
        </set>
        WHERE ${configuration.pkSqlName} = ${r"#"}{${configuration.pkName}}
    </update>

    <!-- 根据条件更新数据 -->
    <update id="updateByWhere">
        UPDATE ${configuration.tableName}
        <set>
            <#list columnList as column>
                <#if (configuration.pkSqlName != column.actualColumnName)>
                    <if test="item != null and item.${column.javaProperty} != null">
                        ${column.actualColumnName} = ${r"#"}{item.${column.javaProperty}},
                    </if>
                </#if>
            </#list>
        </set>
        <where>
            <#list columnList as column>
                <if test="condition != null and condition.${column.javaProperty} != null">
                    and ${column.actualColumnName} = ${r"#"}{condition.${column.javaProperty}}
                </if>
            </#list>
        </where>
    </update>

        <!-- 根据ID删除数据 -->
        <delete id="deleteByPrimaryKey" parameterType="${configuration.pkTypeJava}">
            DELETE FROM  ${configuration.tableName} WHERE ${configuration.pkSqlName} = ${r"#"}{${configuration.pkName}}
        </delete>

        <!-- 根据条件删除数据 -->
        <delete id="deleteBy" parameterType="${configuration.javaModelPackage}.entity.${configuration.domainObjectName}">
            DELETE FROM ${configuration.tableName}
            <trim prefix="where" prefixOverrides="and|or" >
               <#list columnList as column>
                    <if test="${column.javaProperty} != null and ${column.javaProperty} != ''" >
                        and ${column.actualColumnName} = ${r"#"}{${column.javaProperty}}
                    </if>
                </#list>
            </trim>
        </delete>

        <!-- 根据ID集合批量删除数据 -->
        <delete id="deleteBatch" parameterType="java.util.List">
            DELETE FROM ${configuration.tableName}
            WHERE ${configuration.pkSqlName} in
            <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                ${r"#"}{item}
            </foreach>
        </delete>
</mapper>